package studio.mandysa.jiuwo

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import coil.load
import studio.mandysa.jiuwo.databinding.ImageItemBinding
import studio.mandysa.jiuwo.ktx.Linear
import studio.mandysa.jiuwo.ui.theme.JiuwoTheme

class MainActivity : ComponentActivity() {
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            JiuwoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Column(modifier = Modifier.fillMaxSize()) {
                        val headers = remember {
                            mutableStateListOf<String>()
                        }
                        val models = remember {
                            mutableStateListOf<String>()
                        }
                        val footers = remember {
                            mutableStateListOf<String>()
                        }
                        Linear(
                            modifier = Modifier
                                .fillMaxWidth()
                                .weight(1.0f),
                            headers = headers,
                            models = models,
                            footers = footers
                        ) {
                            /*addCompose<String> {
                                CardItem(img = model)
                            }*/
                            addViewBinding<String, ImageItemBinding> { vb ->
                                vb.run {
                                    img.load(model) {
                                        placeholder(getDrawable(coil.compose.singleton.R.color.vector_tint_theme_color))
                                    }
                                    pos.text = "${modelPosition}"
                                }
                            }
                            //需要看其中一个请注释另外一个，因为它们的model类型相同
                        }
                        Row(modifier = Modifier.fillMaxWidth()) {
                            Button(modifier = Modifier.weight(1.0f), onClick = {
                                headers.add("https://p1.music.126.net/Bx_y8fFEF53eNkyBCh27bA==/109951165157087187.jpg?param=640y300")
                            }) {
                                Text("头部：头像")
                            }
                            Button(modifier = Modifier.weight(1.0f), onClick = {
                                models.add("https://p1.music.126.net/fk3myS75uvu4M5-ZwgiucA==/109951166119687855.jpg?param=130y130")
                            }) {
                                Text("默认：月无眠")
                            }
                            Button(modifier = Modifier.weight(1.0f), onClick = {
                                footers.add("https://p1.music.126.net/UCu7GxHsqXnlDwOXug8XyQ==/109951165393051252.jpg?param=130y130")
                            }) {
                                Text("尾部：逍遥戏")
                            }
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun CardItem(img: String) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .height(80.dp)
            .padding(5.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxSize()
                .padding(2.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            AsyncImage(model = img, contentDescription = null, modifier = Modifier.size(60.dp))
            Column {
                Text(text = "这是一个Item")
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    JiuwoTheme {
        Greeting("Android")
    }
}